package com.kmxd.ams.infra.tmpl.tmpl;

import cn.hutool.core.util.ObjectUtil;
import com.kmxd.ams.core.common.constant.TmplConst;
import com.kmxd.ams.infra.arrange.entity.Document;
import com.kmxd.ams.infra.arrange.entity.Project;
import com.kmxd.ams.infra.tmpl.dto.ArchTmplBO;
import com.kmxd.ams.infra.tmpl.dto.JsonFieldDto;
import com.kmxd.ams.infra.tmpl.dto.RuleConfigBO;
import com.kmxd.ams.infra.tmpl.enums.TmplRes;
import com.kmxd.ams.infra.tmpl.util.TmplCommon;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 返回文件级模板
 *
 * @author ZuoLG
 */
@Slf4j
@Service("project_document")
public class TmplProjectDocument implements AbstractTmpl {
  @Autowired private TmplCommon tmplCommon;

  @Override
  public List<ArchTmplBO> getTmpl() {
    List<JsonFieldDto> tableDoc =
        tmplCommon.getTempInfoList(TmplRes.TABLE_DOCUMENT.getPath(), JsonFieldDto.class);
    List<ArchTmplBO> tmpls = Lists.newArrayList();
    ArchTmplBO archTmplDoc = new ArchTmplBO();
    archTmplDoc.setName("通用档案");
    archTmplDoc.setType(ArchTmplBO.DOCUMENT);
    RuleConfigBO ruleConfig = tmplCommon.getConfig(TmplRes.PROJECT_DOCUMENT_RULE_CONFIG);
    archTmplDoc.setConfig(ruleConfig.getDocument());
    List<JsonFieldDto> docFields =
        tmplCommon.getField(TmplCommon.DOC).stream()
            .peek(
                item -> {
                  item.setAttrUse(
                      TmplConst.FROM_PROJ_DOC.contains(item.getCode())
                              || (ObjectUtil.isNotEmpty(item.getAttrUse())
                                  && item.getAttrUse().equals(1))
                          ? 1
                          : 0);
                  item.setAttrEdit(
                      TmplConst.DISABLE_FIELD.contains(item.getCode())
                          ? 0
                          : ObjectUtil.isEmpty(item.getAttrEdit()) ? 0 : item.getAttrEdit());
                  // 设置表格默认宽度级排序
                  JsonFieldDto tableFiel =
                      tableDoc.stream()
                          .filter(x -> x.getCode().equals(item.getCode()))
                          .findFirst()
                          .orElse(null);
                  if (ObjectUtil.isNotNull(tableFiel)) {
                    item.setSortList(
                        ObjectUtil.isNotEmpty(tableFiel.getSortList())
                            ? tableFiel.getSortList()
                            : item.getSortList());
                    item.setHeaderWidth(
                        ObjectUtil.isNotEmpty(tableFiel.getHeaderWidth())
                            ? tableFiel.getHeaderWidth()
                            : item.getHeaderWidth());
                    item.setAttrList(
                        ObjectUtil.isNotEmpty(tableFiel.getHeaderWidth()) ? 1 : item.getAttrList());
                  }
                })
            .collect(Collectors.toList());
    archTmplDoc.setFields(docFields);
    archTmplDoc.setClazz(Document.class);
    tmpls.add(archTmplDoc);
    ArchTmplBO archTmplProj = new ArchTmplBO();
    archTmplProj.setName("通用档案");
    archTmplProj.setType(ArchTmplBO.PROJECT);
    archTmplProj.setConfig(ruleConfig.getProject());
    archTmplProj.setHaveProject(true);
    List<JsonFieldDto> projFields = new ArrayList<>(tmplCommon.getField(TmplCommon.PROJ));
    archTmplProj.setFields(projFields);
    archTmplProj.setClazz(Project.class);
    tmpls.add(archTmplProj);
    return tmpls;
  }
}
